//接收
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <ArduinoJson.h>
//接收数据保存的结构体和发送方一致
typedef struct {
    int16_t accelX;
    int16_t accelY;
    int16_t accelZ;
    int16_t gyroX;
    int16_t gyroY;
    int16_t gyroZ;
} SensorData;

//定义了OnDataRecv函数，当接收到数据时，将数据从缓冲区复制到myData结构体，并使用ArduinoJson库将数据转换为JSON格式，然后通过串口打印输出。
void onDataReceived(uint8_t *mac, uint8_t *data, uint8_t len) {
    SensorData receivedData;memcpy(&receivedData, data, sizeof(receivedData));
    StaticJsonDocument<200> jsonDoc;
    jsonDoc["accelX"] = receivedData.accelX;
    jsonDoc["accelY"] = receivedData.accelY;
    jsonDoc["accelZ"] = receivedData.accelZ;
    jsonDoc["gyroX"] = receivedData.gyroX;
    jsonDoc["gyroY"] = receivedData.gyroY;
    jsonDoc["gyroZ"] = receivedData.gyroZ;
  //将JSON文档序列化为字符串
  serializeJson(jsonDoc, jsonString);
  Serial.println(jsonString);
}

//在接收端ESP8266上，将接收端初始化，设备设置为从设备。
void setup() {
  Serial.begin(115200);
  
//将WiFi设置为STA模式
  WiFi.mode(WIFI_STA);

//初始化 ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("ESP-NOW init failed");
    return;
  }
  
//设置ESP8266为从设备
  esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
  esp_now_register_recv_cb(onDataReceived);
}

void loop(){
  
  
}